﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.DALFactory;
using AspNetCs.IDAL;
using AspNetCs.Model;
using System.Data;
using System.Web;
using AspNetCs.Common;
using System.Data.SqlClient;
using System.Collections.Specialized;
using AspNetCs.Enumerations;
namespace AspNetCs.Bll
{
    public class Users
    {
        private IUser User_DAL = Conn.CreateUsers();

        public int updatePwd(Users_Model model)
        {
            if (model == null) return -1;
            return User_DAL.updatePwd(model);
        }
        public int update(Users_Model model)
        {

            if (model == null) return -1;
            return User_DAL.update(model);
        }

        /// <summary>
        /// 更新用户(后台修改)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AdminUpdate(Users_Model model)
        {
            return User_DAL.AdminUpdate(model);
        }

        public int IsObjUserName(string Un)
        {
            if (Un == "") return -1;
            return User_DAL.IsObjUserName(Un);
        }
        public int Add(Users_Model model)
        {
            if (model == null) return -1;
            return User_DAL.Add(model);
        }
        /// <summary>
        /// 根据用户ID返回用户信息
        /// </summary>
        /// <param name="Userid"></param>
        /// <returns></returns>
        public Users_Model GetModel(int Userid)
        {

            Users_Model model = new Users_Model();
            try
            {
                SqlParameter[] sp = new SqlParameter[1];
                sp[0] = new SqlParameter("@id", SqlDbType.Int);
                sp[0].Value = Userid;

                DataTable dt = User_DAL.GetDataTable(" and Userid=@id", sp);
                foreach (DataRow dr in dt.Rows)
                {
                    model = User_DAL.GetModel(dr);
                }
            }
            finally
            {

            }
            return model;
        }

        /// <summary>
        /// 根据用户名返回用户信息
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public Users_Model GetModel(string UserName)
        {

            Users_Model model = new Users_Model();
            try
            {
                SqlParameter[] sp = new SqlParameter[1];
                sp[0] = new SqlParameter("@UserName", SqlDbType.VarChar);
                sp[0].Value = UserName;
                DataTable dt = User_DAL.GetDataTable(" and UserName=@UserName", sp);
                foreach (DataRow dr in dt.Rows)
                {
                    model = User_DAL.GetModel(dr);
                }
            }
            finally
            {

            }
            return model;
        }


        /// <summary>
        /// 返回所有用户的列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataTable()
        {
            SqlParameter[] sp = new SqlParameter[0];
            return User_DAL.GetDataTable("", sp);
        }
        /// <summary>
        /// 用户登录,返回结果[-2=用户锁定，-1=未知错误，0=用户名，密码不正确，1=登录成功]
        /// </summary>
        /// <param name="Un">用户名</param>
        /// <param name="Pwd">密码[加密后的]</param>
        /// <param name="ExpTime">过期时间</param>
        /// <param name="UserAddIntegral">用户登录成功后所添加的积分</param>
        /// <param name="ErrMsg">返回错误信息</param>
        /// <returns>返回结果[-2=用户锁定，-1=未知错误，0=用户名，密码不正确，1=登录成功]</returns>
        public int UserLogin(string Un, string Pwd, int ExpTime, int UserAddIntegral, out string ErrMsg)
        {
            ErrMsg = "ERROR";
            Users_Model mdl = new Users_Model();
            mdl.Pwd = Pwd;
            mdl.UserName = Un;
            int Result = User_DAL.UserLogin(mdl);
            if (Result == -2)
            {
                ErrMsg = "对不起，该用户已经锁定，具体原因请联系管理员！";
            }
            else if (Result == 0)
            {
                ErrMsg = "用户名，密码输入错误!";
            }
            else if (Result == -1)
            {
                ErrMsg = "未知错误！";
            }
            else if (Result == 1)
            {
                ErrMsg = "登录成功";
                Users_Model UserMd = GetModel(Un);
                Integral_Model md = new Integral_Model();
                md.Types = "Add";
                md.UserId = UserMd.UserId;
                md.Readme = "用户(" + Un + ")登录成功,增加积分";
                md.Num = UserAddIntegral;
                Integral.CreateNew.Add(md);

                UserSave_Property property = new UserSave_Property();
                NameValueCollection ValueList = new NameValueCollection();
                ValueList.Add(property.UserName, UserMd.UserName);
                ValueList.Add(property.UserId, UserMd.UserId.ToString());
                ValueList.Add(property.Email, UserMd.UserEmail.ToString());
                ValueList.Add(property.Purview, ((int)UserTypeEnum.User).ToString());
                SaveType_Factory.Create().WriteUserInfo(ValueList, ExpTime);
            }

            return Result;
        }

        /// <summary>
        /// 更新最后登录成功的时间,与IP
        /// </summary>
        /// <param name="Filter">更新条件，最开始需要加AND,不要加WHERE</param>
        public void UpdateLastLoginTime(string Filter)
        {
            User_DAL.UpdateLastLoginTime(Filter);
        }
        /// <summary>
        /// 更新用户最后退出时间
        /// </summary>
        /// <param name="Filter">更新条件，最开始需要加AND,不要加WHERE</param>
        public void UpdateLastOutTime(string Filter)
        {
            User_DAL.UpdateLastOutTime(Filter);
        }

        public void CheckUserOnline(string Filter, int checkTime_Int)
        {

            User_DAL.CheckUserOnline(Filter, checkTime_Int);
        }

        public int Del(int id)
        {
            return User_DAL.Del(id);
        }

        public int UserLock(int UserId)
        {
            return User_DAL.UserLock(UserId);
        }

        public int UserUnLock(int UserId)
        {
            return User_DAL.UserUnLock(UserId);
        }


        /// <summary>
        /// 扣除用户点数
        /// </summary>
        /// <param name="Point">扣除的点数</param>
        /// <param name="UserId">用户编号</param>
        public void DrePoint(int Point, int UserId)
        {
            User_DAL.DrePoint(Point, UserId);
        }

    }
    public sealed class User_Bll
    {


        /// <summary>
        /// 实例化该类
        /// </summary>
        public static Users CreateNew
        {
            get
            {
                return new Users();
            }
        }

    }

}
